<?php
namespace app\index\controller;
use app\index\controller\Base;
use think\Session;
use think\Db;
class Publish extends Base{
    // 我的文章
    public function index(){		
        if(!Session::get('username')){
    		$this->success('请先登陆！','Reg/index');
    	}else{
			$username=Session::get('username');
			//获取到登陆用户的id
			$user=db('user')->where('username',$username)->column('id');
			$userid=$user[0];
			// var_dump($userid);
			// exit;
    		$info=db('user')->where('username',$username)->select();
			$this->assign('info',$info);			
			$pubs=Db::table('produce')->alias('a')
			->join('user b','a.userid=b.id')
			->field('a.pid,a.title,a.author,a.desc,a.keywords,a.content,a.pic,a.click,a.thumb,a.time,a.userid,b.user_city,b.upic')
			->where('userid',$userid)
			->order('a.pid','desc')
			->paginate(3);
			$page=$pubs->render();
			$this ->assign('page',$page);		
			$this->assign('pubs',$pubs);
			return $this->fetch('personal/publish');			
		}
	}	
    public function editl(){		
		$pid=input('pid');
		$xi=db('produce')->where('pid',$pid)->select();
		$this->assign('xi',$xi);
		return $this->fetch('personal/edit');
	}
    public function edit(){
	$pid=input('pid');
	$author=Session::get('username');	
	  if(request()->isPost()){
			  // dump($_POST); die;
			  $data=[
				  'title'=>input('title'),
				  'author'=>$author,
				  'desc'=>input('desc'),
				  'keywords'=>str_replace('，', ',', input('keywords')),//关键字中英文逗号转换
				  'content'=>input('content'),
				  'time'=>time(),
			  ];

			  if(input('state')=='on'){
				  $data['state']=1;
			  }//是否推荐
			  if($_FILES['pic']['tmp_name']){
				  $file = request()->file('pic');
				  $info = $file->move(ROOT_PATH . 'public' . DS . 'static/uploads');
				  $data['pic']='/uploads/'.$info->getSaveName();
			  }//是否上传照片  
			  if(db('Produce')->where('pid',$pid)->update($data)){
				  return $this->success('修改文章成功！','personal/index');
			  }else{
				  return $this->error('修改文章失败！');
			  }
			  return;
		  }
		//   $cateres=db('cate')->select();
		//   $this->assign('cateres',$cateres);
		  return $this->fetch();
	}
	public function del(){
    	$pid=input('pid');
		if(db('produce')->delete(input('pid'))){
			$this->success('删除文章成功！','personal/index');
		}else{
			$this->error('删除文章失败！');
		}
    	
    }
}